package com.zbdx.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zbdx.bean.Course;
import com.zbdx.bean.Teacher;
import com.zbdx.bean.teacherDto;
import com.zbdx.dao.teacherDao;
import com.zbdx.dao.impl.teacherDaoImpl;

/**
 * Servlet implementation class TeacherServlet
 */
@WebServlet("/TS")
public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TeacherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String method = request.getParameter("method");
    	if ("findAllTeacher".equals(method)) {
    		findALLTeacher(request, response);
		}
    	else if ("insertTeacher".equals(method)) {
			insertTeacher(request, response);
		}
    	else if ("loginTeacher".equals(method)) {
			loginTeacher(request, response);
		}
    	else if ("updateTeacher".equals(method)) {
			updateTeacher(request, response);
		}
    	
    	else if ("addCourse".equals(method)) {
			addCourse(request, response);
		}
    	else if ("deleteCourse".equals(method)) {
			deleteCourse(request, response);
		}
    	else if("findALLCourse".equals(method)) {
    		findALLCourse(request, response);
    	}
    	else if ("findAllScore".equals(method)) {
			findAllScore(request, response);
		}
    	else if("findTeacherbyName".equals(method)){
    		findTeacherbyName(request, response);
		}
	}
    
    
    
    
	protected void findALLTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		teacherDao tDao = new teacherDaoImpl();
		List<Teacher> list = tDao.findAllteacher();
		request.setAttribute("tlist", list);
		request.getRequestDispatcher("teacher_list.jsp").forward(request, response);
	}

	
	protected void insertTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		int collgegid = Integer.parseInt(request.getParameter("collegeid"));
		int schoolid = Integer.parseInt(request.getParameter("schoolid"));
		String teachername = request.getParameter("name");
		teachername = new String(teachername.getBytes("ISO-8859-1"),"UTF-8");
		String password = request.getParameter("password");
		String idcard = request.getParameter("idCard");
		String sex = request.getParameter("sex");
		sex = new String(sex.getBytes("ISO-8859-1"),"UTF-8");
		String teacherTitle = request.getParameter("teacherTitle");
		teacherTitle = new String(teacherTitle.getBytes("ISO-8859-1"),"UTF-8");
		String entrydate = request.getParameter("EntryDate");
		String birther = request.getParameter("Birther");
		String stuatus = request.getParameter("Stuatus");
		stuatus = new String(stuatus.getBytes("ISO-8859-1"),"UTF-8");
		String tel = request.getParameter("tel");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		address = new String(address.getBytes("ISO-8859-1"),"UTF-8");
		Teacher teacher = new Teacher(teachername, password, idcard, sex, teacherTitle, entrydate, birther, stuatus, tel, email, address, collgegid, schoolid);
		//System.out.println(teacher);
		teacherDao tDao = new teacherDaoImpl();
		tDao.insertTeacher(teacher);
		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.getRequestDispatcher("teacher_update.jsp").forward(request, response);
	}
	
	protected void loginTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//System.out.println("login");
		String name = request.getParameter("name");
		name = new String(name.getBytes("ISO-8859-1"),"utf-8");
		String password = request.getParameter("password");
		password = new String(password.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println("hello"+name+","+password);
		teacherDao tDao = new teacherDaoImpl();
		boolean flag =tDao.login(name, password);
		if (flag) {
			response.sendRedirect(request.getContextPath()+"/main.html");
		}else {
			response.sendRedirect(request.getContextPath()+"/error.html");
		}
	}
	
	protected void updateTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String teachername = request.getParameter("teachername");
		teachername = new String(teachername.getBytes("ISO-8859-1"),"UTF-8");
		String teachertitle = request.getParameter("teacherTitle");
		teachertitle = new String(teachertitle.getBytes("ISO-8859-1"),"UTF-8");
		String entrydate = request.getParameter("EntryDate");
		String birther = request.getParameter("Birther");
 		String stuatus = request.getParameter("Stuatus");
 		stuatus = new String(stuatus.getBytes("ISO-8859-1"),"UTF-8");
 		String tel = request.getParameter("tel");
 		Teacher teacher = new Teacher(teachername, teachertitle, entrydate, birther, tel, stuatus);
 		//System.out.println(teacher);
		teacherDao tDao = new teacherDaoImpl();
		tDao.updateTeacher(teacher);
		request.getRequestDispatcher("teacher_update.jsp").forward(request, response);
	}
	
	protected void addCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		teacherDao tDao =new teacherDaoImpl();
		int teacherid = Integer.parseInt(request.getParameter("teacherid"));
		String coursename =request.getParameter("coursename");
		coursename = new String(coursename.getBytes("ISO-8859-1"),"UTF-8");
		String room =request.getParameter("room");
		room = new String(room.getBytes("ISO-8859-1"),"UTF-8");
		int Max = Integer.parseInt(request.getParameter("Max"));
		int college = Integer.parseInt(request.getParameter("college"));
		Course course = new Course(teacherid, coursename, room, Max, college);
		boolean flag = tDao.addCoures(course);
		if (flag) {
			request.getRequestDispatcher("course_add.jsp").forward(request, response);
		}
	}
	
	protected void findAllScore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		teacherDao tDao =new teacherDaoImpl();
		List<Course> list = tDao.findAllcourse();
		request.setAttribute("tScore", list);
		request.getRequestDispatcher("score_list.jsp").forward(request, response);
	}
	
	protected void deleteCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String coursename = request.getParameter("courseName");
		coursename = new String(coursename.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println(coursename);
		teacherDao tDao =new teacherDaoImpl();
		tDao.deleteCourse(coursename);
		System.out.println("deleteCourse");
		//response.sendRedirect("/TS?method=findALLCourse");
	}
	
	protected void findALLCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		teacherDao tDao =new teacherDaoImpl();
		List<teacherDto> list = tDao.selectCourse();
		request.setAttribute("tCourse", list);
		request.getRequestDispatcher("course_list.jsp").forward(request, response);
	}
	
	protected void findTeacherbyName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String teachname = request.getParameter("teachername");
		teachname = new String(teachname.getBytes("ISO-8859-1"),"UTF-8");
		//System.out.println(teachname);
		teacherDao tDao = new teacherDaoImpl();
		Teacher list = tDao.findbyName(teachname);
		request.setAttribute("tnlist", list);
		request.getRequestDispatcher("teacher_update.jsp").forward(request, response);
	}
	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	

}
